<template>
	<div class="jobsManager w944">
		<alert ref="alert"></alert>
		<deleteAlert ref="deleteAlert"></deleteAlert>
		<div class="label_top">
			<p><i class="line"></i>岗位管理</p>
		</div>
		<div class="label_table">
			<div class="el-tabs-report"> 
				<a class="listOne" @click="goaddbiaoqian">新增岗位</a>
				<a class="listTwo" @click="importDaoChu">导出数据</a>
				<div class="msearch">
					<input placeholder="请输入岗位关键词……" v-model="search"/>
					<a @click="getTableList"><img src="../../assets/qudaoImg/search.png"/></a>
				</div>
           </div>
			<el-table ref="multipleTable" :data="tableData" border @selection-change="handleSelectionChange">
				<el-table-column type="selection"></el-table-column>
				<el-table-column prop="name" label="岗位名称"></el-table-column>
				<el-table-column prop="count" label="使用次数"></el-table-column>
				<el-table-column label="操作">
					<template slot-scope="scope">
						<el-button @click="handleEdit(scope.row)">编辑</el-button>
						<el-button @click="handleDelete(scope.row)" class="red">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<div class="fenyeMargin">
				<el-button @click="toggleSelection(tableData)" class="allSelect">全选</el-button>
				<el-button @click="handleDeleteAll" class="allDelete">批量删除</el-button>
				<div class="pag_block">
					<el-pagination @current-change="handleCurrentChange" :current-page="currentpage" :page-size="pagesize" layout="prev, pager, next" :total="total"></el-pagination>
				</div>
			</div>
			<div class="bangdanWrap" v-show="wrapShow">
				<div class="bd_line">
					<span>岗位名称:</span>
					<input v-model="name" placeholder="请输入岗位名称"/>
				</div>
				<div class="footerButton">
					<a @click="addSubmitSave">保存</a>
					<a @click="cancel" class="cancel">取消</a>
				</div>
			</div>
		</div>
	</div>
</template>
<script>
	import alert from '../../components/Alert.vue';
	import deleteAlert from '../../components/deleteAlert.vue';
	export default {
		data() {
			return {
				department:'',
				search:'',
				nowType:'add',
				tableData: [],
				pagesize: 15,
				currentpage: 1,
				total: 1,
        		allSelect: false,
				multipleSelection: [],
				values:'',
				wrapShow:false,
				name:'',
				deleteType:'delete'
			}
		},
		methods: {
			getTableList() {
				let s = {
					rootcompanyid:1,
					page: this.currentpage,
					num: this.pagesize,
					name: this.search,
					type:2
				};
				// 初始化更新页面
				this.$ajax({
					url: this.$root.jifuUrl + "/mns/department/selectDepartmentList", 
					headers:{'ContentType': 'application/json; charset=utf-8'},
					method: 'POST',
					data: s,
					success: res => {
						if(res.data.code == 200) {
							this.tableData = res.data.data.list;
							this.total = res.data.data.pageParam.items;
						}
					}
				});
			},
			handleEdit(row) {
	 			this.nowType = 'edit';
	 			this.wrapShow = true;
	 			this.id = row.id;
	 			this.name = row.name;
			},
			goaddbiaoqian(){
				this.wrapShow = true;
			},
			addBiaoqian() {
				let addBiaoqianDetail = {
					rootcompanyid:1,
					name: this.name,
					type: 2
				};
				this.$ajax({
					url: this.$root.jifuUrl + "/mns/department/addDepartment",
					headers:{'ContentType': 'application/json; charset=utf-8'},
					method: 'POST',
					data: addBiaoqianDetail,
					success: res => {
						if(res.data.code == 200) {
							this.wrapShow = false;
							this.$rootFunction.addResSuccess();
						}else{
							this.failText = res.data.message;
							this.$rootFunction().deleteResFail();
						}
					}
				});
			},
			updateBiaoqian() {
				let updateBiaoqianDetail = {
					id:this.id,
					rootcompanyid:1, 
					name: this.name,
					type: 2
				};
				this.$ajax({
					url: this.$root.jifuUrl + "/mns/department/updateDepartment",
					headers:{'ContentType': 'application/json; charset=utf-8'},
					method: 'POST',
					data: updateBiaoqianDetail,
					success: res => {
						if(res.data.code == 200) {
							this.wrapShow = false;
							this.$rootFunction.addResSuccess();
						}else{
							this.failText = res.data.message;
							this.$rootFunction().deleteResFail();
						}
					}
				});
			},
			addSubmitSave(){
				if(this.nowType == 'add') {
					this.addBiaoqian();
				}else if(this.nowType == 'edit') {
					this.updateBiaoqian();
				}
			},
			cancel(){
				this.wrapShow = false;
				this.nowType='add';
			},
			handleDelete(row){
				this.transitionRow = row;
				this.$rootFunction().rootDelete();
			},
			handleDeleteAll() {
				this.$rootFunction().rootDeleteAll();
			},
			deleteSubmit() {
				if(this.deleteType == 'delete'){
					var list = [];
					list.push({
						id:this.transitionRow.id,
						type:this.transitionRow.type
					});
					this.$ajax({ 
						url: this.$root.jifuUrl + "/mns/department/deleteDepartment",
						headers:{'ContentType': 'application/json; charset=utf-8'},
						method: 'DELETE',
						data:list,
						success: res => {
							if(res.data.code == 200) {
								this.$rootFunction().deleteResSuccess();
							}else{
								this.failText = res.data.message;
								this.$rootFunction().deleteResFail();
							}
						}
					});
				}else{
					this.$ajax({ 
						url: this.$root.jifuUrl + "/mns/department/deleteDepartment",
						headers:{'ContentType': 'application/json; charset=utf-8'},
						method: 'DELETE',
						data:this.multipleSelection,
						success: res => {
							if(res.data.code == 200) {
								this.$rootFunction().deleteResSuccess();
							}else{
								this.failText = res.data.message;
								this.$rootFunction().deleteResFail();
							}
						}
					});
				}
			},
			toggleSelection(rows) {
			    if(rows) {
			        rows.forEach(row => {
			            this.$refs.multipleTable.toggleRowSelection(row, !this.allSelect)
			        })
			        this.allSelect = !this.allSelect;
			    }
			},
			handleSelectionChange(val) {
			    this.multipleSelection = [];
			    for(var i = 0;i<val.length;i++){
			        var s={id:val[i].id,type:val[i].type}
			        this.multipleSelection.push(s)
			    }
			},
		    handleCurrentChange (value) {
		        this.currentpage = value;
		        this.getTableList();
		    },
			importDaoChu(){}
		},
		components: {
			alert,
			deleteAlert
		},
		mounted: function() {
			this.getTableList();
		    this.department = this.$root.huasan.department;
		}
	}
</script>